<?php

class WebServer {

    public $server;

    public function __construct() {
        $this->server = new swoole_websocket_server("0.0.0.0", 9502);
        $this->server->on('open', [$this, 'onOpen']);
        $this->server->on('message', [$this, 'onMessage']);
        $this->server->on('request', [$this, 'onRequest']);
        $this->server->on('close', [$this, 'onClose']);
        $this->server->start();
    }

    public function onRequest(swoole_http_request $request, swoole_http_response $response) {
        require_once(dirname(__FILE__) . '/index.php');
        $this->formatReqeustParam($request, $response);
        $_SERVER['REQUEST_FLAG'] = 1;
        httpResponse();
    }

    public function formatReqeustParam(swoole_http_request $request, swoole_http_response $response) {
        $_SERVER['REQUEST_METHOD'] = $request->server['request_method'];
        $_SERVER['REQUEST_URI'] = $request->server['request_uri'];
        $_SERVER['REQUEST_TIME'] = $request->server['request_time'];
        $_SERVER['HTTP_RESPONSE'] = $response;
        $_GET = $request->get;
        $_POST = $request->post;
        $_COOKIE = $request->cookie;
        $_FILES = $request->files;
    }

    public function onClose() {
    }

    public function onMessage() {
    }

    public function onOpen() {
    }
}

new WebServer;
